/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/

package nixonftp.list;

import nixonftp.*;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Billy
 */
public class NXListRenderer extends JPanel implements ListCellRenderer {

	private JLabel label;
	private JLabel arrow;

	public NXListRenderer() {
		setOpaque(true);

		BoxLayout box = new BoxLayout(this, BoxLayout.LINE_AXIS);
		setLayout(box);
		label = new JLabel();
		label.setIconTextGap(6);
		label.setAlignmentX(LEFT_ALIGNMENT);
		label.setBorder(BorderFactory.createEmptyBorder(2, 6, 2, 6));
		setBackground(new Color(255,255,255));
		add(label);

		arrow = new JLabel();
		arrow.setBorder(BorderFactory.createEmptyBorder(2, 6, 2, 6));
		add(Box.createHorizontalGlue());
		add(arrow);
	}

	public Component getListCellRendererComponent(JList list, Object value,
			  int index, boolean isSelected, boolean cellHasFocus) {
		//change the path accordingly
		if (isSelected) {
			setBackground(list.getSelectionBackground());
			label.setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
			label.setForeground(list.getForeground());
		}

		int[] indices = list.getSelectedIndices();
		int items = list.getModel().getSize();
		NXObjectIndex oi = null;

		try {
			oi = (NXObjectIndex) value;
			label.setText(oi.name);
			label.setIcon(NXIcon.small[oi.type]);
			
			if (oi.type != 3) {
				String tipText = "<html><font color='#336699'><b>" + oi.name +
						  "</b></font><br><font color='#666666'>" + NXFormat.fileSize(oi.size) +
						  "</font> | " + NXFormat.parseDate(oi.date) ;

				if (indices.length > 0) tipText += "<br>" + indices.length + " of " + items + " items";
				setToolTipText(tipText + "</html>");
			} else {
				File f = new File(oi.name);
				try {
					long free = f.getFreeSpace();
					long total = f.getTotalSpace();

					if (total == 0) {
					setToolTipText("<html><font color='#336699'><b>Drive " + oi.name + "</b></font><br><font color='#666666'>" +
						  "Optical (empty)</font></html>");
					} else {
						String space = NXFormat.fileSize(total);
						double divide = (double) free / (double) total * 100;
						String percent = String.valueOf(Math.round(divide));
						setToolTipText("<html><font color='#336699'><b>Drive " + oi.name + "</b></font><br>" +  NXFormat.fileSize(free) + " free<br><font color='#666666'>" +
							  "(" + percent + "% of " + space + ")</font></html>");
					}
				} catch (NoSuchMethodError e) {
				}
			}

			if (oi.type > 1) {
				if (isSelected) arrow.setIcon(NXIcon.expandSelected);
				else arrow.setIcon(NXIcon.expand);
				arrow.setVisible(true);
			} else {
				arrow.setVisible(false);
			}
		} catch (ClassCastException ex) {
			label.setIcon(NXIcon.small[0]);
			label.setText((String) value);
			arrow.setVisible(false);
		}

		NXList filterList = (NXList) list;
		NXFilter[] filters = filterList.getFilters();
		if (filters != null) {
			NXFilter[] filter = filterList.filter(filters, oi);
			for (int x = 0; x < filter.length; x++) { //iterate through the matches
				if (filter[x] != null) {
					if (filter[x].action == NXFilter.COLOR && filter[x].color != null && !isSelected) {
						setBackground(filter[x].color);
						label.setForeground(NXFormat.invertColor(filter[x].color));
					}
				}
			}
		}

		invalidate();
		return this;
	}

}
